﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;
using StockAdjust;

namespace PresentationLayer
{
    public partial class ApproveStockAdjustmentVoucherList : System.Web.UI.Page
    {
        ApproveStockAdjustmentVoucherControl control;
        User user;

        protected void Page_Load(object sender, EventArgs e)
        {
            control = new ApproveStockAdjustmentVoucherControl();

            // Fake users
            string username = "Supervisor1";
            //string username = "StoreMgr1";
            user = control.GetUserByUsername(username);

            if (!IsPostBack)
            {
                lblMessage.Text = "";
                GenerateGridView(user);
            }
        }

        private void GenerateGridView(User user)
        {
            List<AdjustmentDisplay> list = new List<AdjustmentDisplay>();
            if (user.Role.RoleName.Equals("Store Manager"))
            {
                list = control.GetAllPendingAdjustmentForStoreManager();
            }
            else if (user.Role.RoleName.Equals("Store Supervisor"))
            {
                list = control.GetAllPendingAdjustmentForSupervisor();
            }
            if (list.Count > 0)
            {
                gvApproveList.DataSource = list;
                gvApproveList.DataBind();
            }
            else
            {
                lblMessage.Text = "There is no pending stock adjustment voucher at the moment";
                gvApproveList.Visible = false;
                btnApprove.Visible = false;
            }
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            // Loop through the gridview to find the rows that are checked
            List<int> voucherList = new List<int>();
            foreach (GridViewRow row in gvApproveList.Rows)
            {
                CheckBox checkbox = (CheckBox)row.FindControl("cboVoucher");
                if (checkbox.Checked)
                {
                    Label voucherid = (Label)row.FindControl("lblVoucherId") as Label;
                    voucherList.Add(Convert.ToInt32(voucherid.Text));
                }
            }

            if (voucherList.Count > 0)
            {
                // Update ApprovalBy, ApprovalDate and Status to database
                string redirectURL = control.UpdateVouchers(user, voucherList);

                // Save to HttpSession
                Session["AdjustmentDetail"] = voucherList;

                // Redirect to successful page
                Response.Redirect(redirectURL);
            }
            else
            {
                lblMessage.Text = "No stock adjustment voucher is selected";
            }
        }

        protected void lbtnViewDetail_Click(object sender, EventArgs e)
        {
            GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
            Label lblVoucherId = (Label)clickedRow.FindControl("lblVoucherId");
            List<int> voucherId = new List<int>();
            voucherId.Add(Convert.ToInt32(lblVoucherId.Text));

            // Save to HttpSession
            Session["AdjustmentDetail"] = voucherId;

            // Redirect to view details page
            string redirectURL = control.ViewAdjustmentDetail(voucherId);
            Response.Redirect(redirectURL);
        }

    }
}